import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: AliceMana
 * Date: 2022-12-31
 * Time: 1:21
 */
public class Solution {
    // https://leetcode.cn/problems/happy-number/
    public static boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while (n != 1) {
            int ret = 0;
            while (n != 0) {
                ret += Math.pow(n%10, 2);
                n /= 10;
            }
            if (!set.contains(ret)) {
                set.add(ret);
            } else {
                return false;
            }
            n = ret;
        }
        return true;
    }

    public static void main1(String[] args) {
        System.out.println(isHappy(2));
    }
}
